package com.webmining.sherlock.web;

import com.webmining.sherlock.admin.Configuration;
import com.webmining.sherlock.admin.ConfigurationException;
import com.webmining.sherlock.admin.Manager;
import org.apache.log4j.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class SherlockInitializerServlet extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        // Initialize the Sherlock environment
        try {
            this.log("Starting Sherlock");
            Manager.initEnvironment();
        } catch (ConfigurationException ce) {
            if (Configuration.isLoggerActive()) {
                Logger log = Logger.getLogger(SherlockInitializerServlet.class);
                log.error("Unable to initialize sherlock", ce);
            } else {
                this.log("Unable to initialize sherlock", ce);
                throw new ServletException("Unable to initialize sherlock", ce);
            }
        }
    }

    public void destroy() {
        this.log("Stopping Sherlock");
        Manager.shutdownEnvironment();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }

}
